<template>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable>
          <el-option
            v-for="dict in dict.type.data_base_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="驱动类" prop="driver">
        <el-input
          v-model="queryParams.driver"
          placeholder="请输入驱动类"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="连接串样例" prop="sample">
        <el-input
          v-model="queryParams.sample"
          placeholder="请输入连接串样例"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数据库名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入数据库名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="链接模板" prop="url">
        <el-input
          v-model="queryParams.url"
          placeholder="请输入链接模板"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.data_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['console:databaseType:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['console:databaseType:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['console:databaseType:remove']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['console:databaseType:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar
        :show-search.sync="showSearch"
        @queryTable="getList"
      />
    </el-row>

    <el-table
      v-loading="loading"
      :data="databaseTypeList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" align="center" />
      <!-- <el-table-column label="序号" align="center" width="55">
        <template slot-scope="scope">
          <span v-text="getIndex(scope.$index)"></span>
        </template>
      </el-table-column> -->
      <el-table-column label="编号" prop="id" align="center" />
      <el-table-column label="任务名称" align="center" prop="taskName" />
      <el-table-column label="任务模式" align="center" prop="taskType" />
      <el-table-column label="任务类型" align="center" prop="jobType">
        <template slot-scope="scope">
          <div v-for="dict in dict.type.synchronous_tasktype" :key="dict.value">
            <!-- {{ dict.value ==  ? dict.label : "" }} -->
            {{ dict.value == scope.row.jobType ? dict.label : "" }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="源实例名" align="center" prop="sourceName" />
      <el-table-column label="目标实例名" align="center" prop="sinkName" />
      <el-table-column label="任务状态" align="center" prop="taskStatus">
        <template slot-scope="scope">
          <span v-for="dict in dict.type.taskstatus" :key="dict.value">
            <el-tag
              v-if="scope.row.taskStatus == dict.value"
              :type="dict.raw.listClass"
            >
              {{ dict.label }}
            </el-tag>
          </span>
        </template>
      </el-table-column>

      <el-table-column
        label="更新时间"
        width="150"
        align="center"
        prop="updateTime"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['console:databaseType:edit']"
            size="mini"
            type="text"
            style="color: red"
            icon="el-icon-switch-button"
            @click="handleStopTask(scope.row)"
          >停止</el-button>
          <el-button
            v-hasPermi="['console:databaseType:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pgNum"
      :limit.sync="queryParams.pgSize"
      @pagination="getList"
    /> -->
  </div>
</template>

<script>
import {
  listTaskProgressconf,
  DataTaskconfTaskStop,
  delDataTaskconf
} from '@/api/console/dataTaskconf'

export default {
  name: 'DatabaseType',
  dicts: [
    'data_status',
    'data_base_type',
    'synchronous_tasktype',
    'taskstatus'
  ],
  beforeRouteEnter(to, from, next) {
    next((vm) => {
      vm.getList()
      vm.listTimer = setInterval(() => {
        vm.getList()
      }, 3000)
    })
  },
  beforeRouteLeave(to, from, next) {
    clearInterval(this.listTimer)
    this.listTimer = null
    next()
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 数据库类型表格数据
      databaseTypeList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pgNum: 1,
        pgSize: 10,
        taskName: null,
        taskType: null,
        sourceName: null,
        sinkName: null,
        status: null,
        taskStatus: 0
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  methods: {
    getIndex($index) {
      return (
        (this.queryParams.pgNum - 1) * this.queryParams.pgSize + $index + 1
      )
    },
    /** 查询数据库类型列表 */
    getList() {
      this.loading = true
      listTaskProgressconf()
        .then((response) => {
          this.databaseTypeList = response.data
          this.loading = false
        })
        .catch((err) => {
          if (err) {
            this.loading = false
            clearInterval(this.listTimer)
            this.listTimer = null
          }
        })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        driver: null,
        sample: null,
        name: null,
        url: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pgNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加数据库类型'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getDatabaseType(id).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改数据库类型'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateDatabaseType(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addDatabaseType(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal
        .confirm('是否确认删除任务配置编号为"' + ids + '"的数据项？')
        .then(function() {
          return delDataTaskconf({ idArray: Array.isArray(ids) ? ids : [ids] })
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'console/databaseType/export',
        {
          ...this.queryParams
        },
        `databaseType_${new Date().getTime()}.xlsx`
      )
    },
    handleStopTask(row) {
      const ids = row.id || this.ids
      this.$modal
        .confirm('是否确认停止任务配置编号为"' + row.jobId + '"的数据项？')
        .then(function() {
          return DataTaskconfTaskStop({ id: row.jobId })
        })
        .then((res) => {
          this.getList()
          this.$modal.msgSuccess(res.data.message)
        })
        .catch(() => {})
    }
  }
}
</script>
